package com.spider.admin.conf;

import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.core.CheckedConsumer;
import org.elasticsearch.xcontent.NamedXContentRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.List;

@Configuration
public class ESConfig {


    @Value("${elasticsearch.host}")
    private String esIp;
    @Value("${elasticsearch.port}")
    private String esPort;
    @Value("${elasticsearch.userName}")
    private String esUsername;
    @Value("${elasticsearch.password}")
    private String esPassword;
    @Value("${elasticsearch.scheme}")
    private String esReqSchema;


    @Bean(name = "esClient")
    public RestHighLevelClient restHighLevelClient() {

        HzRestHighLevelClient restHighLevelClient;
//        if (StringUtils.isAnyBlank(this.esUsername, this.esPassword)) {
        // 使用 CredentialsProvider 对象登陆
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials
                (AuthScope.ANY, new UsernamePasswordCredentials(this.esUsername, this.esPassword));
        HttpHost http = new HttpHost(esIp, Integer.parseInt(this.esPort), this.esReqSchema);
        restHighLevelClient =
                new HzRestHighLevelClient(RestClient.builder(
                                http)
                        .setHttpClientConfigCallback(
                                httpAsyncClientBuilder ->
                                        httpAsyncClientBuilder.disableAuthCaching()
                                                .setDefaultCredentialsProvider(credentialsProvider)
                        ));
        restHighLevelClient.ip = esIp;
        restHighLevelClient.port = Integer.parseInt(this.esPort);
        restHighLevelClient.username = this.esUsername;
        restHighLevelClient.password = this.esPassword;
        restHighLevelClient.needAuth = Boolean.TRUE;
//        } else {
//
//            restHighLevelClient =
//                    new HzRestHighLevelClient(RestClient.builder(
//                            new HttpHost(esIp, Integer.parseInt(this.esPort), this.esReqSchema)));
//        }
        return restHighLevelClient;
    }

    public static class HzRestHighLevelClient extends RestHighLevelClient {
        @Getter
        private String username;
        @Getter
        private String password;
        @Getter
        private String ip;
        @Getter
        private int port;
        @Getter
        private boolean needAuth;

        public HzRestHighLevelClient(RestClientBuilder restClientBuilder) {
            super(restClientBuilder);
        }

        protected HzRestHighLevelClient(RestClientBuilder restClientBuilder, List<NamedXContentRegistry.Entry> namedXContentEntries) {
            super(restClientBuilder, namedXContentEntries);
        }

        protected HzRestHighLevelClient(RestClient restClient, CheckedConsumer<RestClient, IOException> doClose, List<NamedXContentRegistry.Entry> namedXContentEntries) {
            super(restClient, doClose, namedXContentEntries);
        }
    }

}
